<template>
  <div class="container">
    <el-container>
      <!-- 头部 -->
      <el-header>
        <!-- 头像 -->
        <div>
          <img src="../../assets/logo.jpg" alt />
          <span>后台管理平台</span>
        </div>
        <!-- 退出 -->
        <el-button plain type="info" @click="logout">退出登录</el-button>
      </el-header>
      <!-- 侧边栏 -->
      <el-container>
        <el-aside :width=" isCollapse ? '64px' : '200px'  ">
          <el-menu
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active='activePath'
          >
          <div class="toggle-collapse" @click="toggleCollapse">|||</div>
          <!-- 一级路由 -->
          <!-- index只接收字符串 -->
            <el-submenu :index="item.id+''"
              v-for ="item in menuList"
              :key ='item.id'
            >
              <!-- 一级菜单模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconsObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级路由 -->
                <el-menu-item
                 :index="'/'+subItem.path"
                 v-for="subItem in item.children"
                 :key='subItem.id'
                 @click="setActiveState('/'+subItem.path)"
                >
                  <template slot="title">
                    <!-- 图标 -->
                    <i class="el-icon-menu"></i>
                    <!-- 文本 -->
                    <span>{{subItem.authName}}</span>
                  </template>
                </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主体部分 -->
        <el-main>
          <router-view/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { getMenuLists } from '@api'
export default {
  data () {
    return {
      // 接收侧边栏导航数据
      menuList: [],
      // 一级导航字体图标
      iconsObj: {
        125: 'iconfont icon-yonghu',
        103: 'iconfont icon-quanxian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-icon-',
        145: 'iconfont icon-shujutongji'
      },
      // 侧边栏展开设置
      isCollapse: false,
      // 保存当前二级路由路径
      activePath: ''
    }
  },
  methods: {
    // 退出
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 请求侧边栏数据
    fetchMenuList () {
      getMenuLists().then(res => {
        if (res.data.meta.status === 200) {
          this.menuList = res.data.data
        } else {
          this.$message.err(res.data.meta.msg)
        }
      })
    },
    // 切换侧边栏展开状态
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 存储当前二级路由路径
    setActiveState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  },
  created () {
    // 获取侧边栏数据
    this.fetchMenuList()
    this.activePath = window.sessionStorage.getItem('activePath') ? window.sessionStorage.getItem('activePath') : ''
  }
}
</script>

<style lang="scss" scoped>
.container {
  height: 100%;
}
.el-container {
  height: 100%;
}

.el-header {
  background-color: #fff;
  color: #333;
  line-height: 60px;
  display: flex;
  font-size: 20px;
  font-weight: 600;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      border: 1px solid #000;
      margin-right: 10px;
    }
    span {
      color: #333;
    }
  }
  .el-button {
    color: #333;
  }
}

.el-aside {
  background-color: #333744;
  color: #fff;
  text-align: center;
}
.el-menu-item {
  padding: 0 20px 0 80px ;
}
.el-main {
  background-color: #e9eef3;
}

body > .el-container {
  margin-bottom: 40px;
}

.iconfont{
  margin-right: 10px;
  color: #fff;
}

.toggle-collapse{
  line-height: 30px;
  font-size: 14px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
}
</style>
